package com.example.post.services.impl;
import com.example.post.mapper.UserMapper;
import com.example.post.model.User;
import com.example.post.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServicesImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserById(int id) {
        return userMapper.findById(id).orElse(null);
    }

    @Override
    public List<User> queryAll() {
        return userMapper.findAll();
    }
    @Override
    public User add(User user) {
        if(userMapper.queryByName(user.getUsername())!=null){
            return null;
        }else{
            userMapper.add(user);
            User newUser = userMapper.queryByName(user.getUsername());
            if(newUser != null){
                newUser.setPassword("");
            }
            return newUser;
        }
}
    @Override
    public User queryByName(String username) {
        return userMapper.queryByName(username);
    }

    @Override
    public User get(String username, String password) {
        return userMapper.getByUsernameAndPassword(username, password);
    }

    @Override
    public List<User> findAllByUsername(String username) {
        return userMapper.findAllByUsername(username);
    }

    @Override
    public void updateUserById(int id, String password) {
        userMapper.updateUserById(id, password);
    }

}
